package com.prawn.ift_baseconfig.service.impl;


import com.prawn.ift_baseconfig.dao.ManufactorDao;
import com.prawn.ift_baseconfig.pojo.Manufactor;
import com.prawn.ift_baseconfig.service.ManufactorService;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.NullAttributeUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/20
 * @DESC： 厂商信息服务接口
 */
@Service
public class ManufactorServiceImpl implements ManufactorService {

    @Autowired
    private ManufactorDao manufactorDao;

    /**
     * 添加厂商信息
     * @param manufactor
     * @return Result
     */
    @Override
    public Result addManufator(Manufactor manufactor) {

       manufactorDao.save(manufactor);
       return Result.success();

    }

    /**
     * 删除厂商信息
     * @param id
     * @return Result
     */
    @Override
    public Result deleteManufator(String id) {
        manufactorDao.deleteById(id);
        return Result.success();
    }

    /**
     * 修改厂商信息
     * @param manufactor
     * @return Result
     */
    @Override
    public Result editManufator(Manufactor manufactor) {


        Manufactor oldManufactor =manufactorDao.findById(manufactor.getId()).get();
        BeanUtils.copyProperties(manufactor,oldManufactor, NullAttributeUtils.getNullPropertyNames(manufactor));



        manufactorDao.save(oldManufactor);
            return Result.success();

    }

    /**
     * 查询厂商详细信息
     * @param id
     * @return Manufactor
     */
    @Override
    public Manufactor getManufatorById(String id) {
        return manufactorDao.findById(id).get();
    }

    /**
     * 查询厂商信息列表
     * @return Result
     */
    @Override
    public Result getManufatorList() {

        List<Manufactor> manufactorList = manufactorDao.findAll();
        return Result.success().add(manufactorList);
    }

    /**
     * 分页查询厂商信息列表
     * @return Result
     */
    @Override
    public Result getManufatorPage(int page, int rows) {
//        Specification<Manufactor> specification = createSpecification(whereMap);
        PageRequest pageRequest =  PageRequest.of(page-1, rows);
        Page<Manufactor> p= manufactorDao.findAll(pageRequest);
        PageResult<Manufactor> pageResult=new PageResult<>(p.getTotalElements(),p.getContent());
        return Result.success().add(pageResult);
    }


    /**
     * 动态条件构建
     * @param searchMap
     * @return
     */
    private Specification<Manufactor> createSpecification(Map searchMap) {

        return new Specification<Manufactor>() {

            @Override
            public Predicate toPredicate(Root<Manufactor> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编号
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                    predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 名称
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                    predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 联系人
                if (searchMap.get("linkman")!=null && !"".equals(searchMap.get("linkman"))) {
                    predicateList.add(cb.like(root.get("linkman").as(String.class), "%"+(String)searchMap.get("linkman")+"%"));
                }
                // 厂家详细地址
                if (searchMap.get("address")!=null && !"".equals(searchMap.get("address"))) {
                    predicateList.add(cb.like(root.get("address").as(String.class), "%"+(String)searchMap.get("address")+"%"));
                }
                // 联系电话
                if (searchMap.get("telphone")!=null && !"".equals(searchMap.get("telphone"))) {
                    predicateList.add(cb.like(root.get("telphone").as(String.class), "%"+(String)searchMap.get("telphone")+"%"));
                }
                // 传真
                if (searchMap.get("fax")!=null && !"".equals(searchMap.get("fax"))) {
                    predicateList.add(cb.like(root.get("fax").as(String.class), "%"+(String)searchMap.get("fax")+"%"));
                }
                // 手机号码
                if (searchMap.get("mobile")!=null && !"".equals(searchMap.get("mobile"))) {
                    predicateList.add(cb.like(root.get("mobile").as(String.class), "%"+(String)searchMap.get("mobile")+"%"));
                }
                // 备注信息
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                    predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
                // 创建者
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                    predicateList.add(cb.like(root.get("createBy").as(String.class), "%"+(String)searchMap.get("createBy")+"%"));
                }
                // 更新者
                if (searchMap.get("updateBy")!=null && !"".equals(searchMap.get("updateBy"))) {
                    predicateList.add(cb.like(root.get("updateBy").as(String.class), "%"+(String)searchMap.get("updateBy")+"%"));
                }
                // 删除标记
                if (searchMap.get("delFlag")!=null && !"".equals(searchMap.get("delFlag"))) {
                    predicateList.add(cb.like(root.get("delFlag").as(String.class), "%"+(String)searchMap.get("delFlag")+"%"));
                }

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }

}
